﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using System.Globalization;
using University.Workshop.Model;

namespace University.Workshop.WebApp
{
    public partial class ProductRecommendation : System.Web.UI.Page
    {
        public static Collection<Product> products = new Collection<Product>();
        protected void Page_Load(object sender, EventArgs e)
        {
            productId.Text = Request["ProductID"];
            Name.Text = Request["ProductName"];
            Category.Text = Request["ProductCategory"];
            string[] wordList = null;
            if (Name.Text != null)
            {
                wordList = Name.Text.Split(' ');
            }
            //For datagrid
            try
            {
                
                    products = ProductRecommendationManager.SearchProductRecommendation(wordList, Category.Text, Convert.ToInt64(productId.Text));
                    ProductGrid.DataSource = products;
                    ProductGrid.DataBind();
                
            }
            catch (Exception ex)
            {
                message.Text = ex.Message;
            }
            
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductSelected.aspx?ProductID=" + productId.Text + "&ProductName=" + Name.Text + "&ProductCategory=" + Category.Text);
        }
       
        //For datagrid
        protected void OnPageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ProductGrid.CurrentPageIndex = e.NewPageIndex;
            try
            {
                string[] wordList = null;
                if (Name.Text != null)
                {
                    wordList = Name.Text.Split(' ');
                }
                products = ProductRecommendationManager.SearchProductRecommendation(wordList, Category.Text, Convert.ToInt64(productId.Text));
                ProductGrid.DataSource = products;
                ProductGrid.DataBind();
            }
            catch (Exception ex)
            {
                message.Text = ex.Message;
            }
        }

        protected void ProductGrid_EditCommand(object source, DataGridCommandEventArgs e)
        {
            int productSelected = e.Item.ItemIndex + (ProductGrid.CurrentPageIndex * ProductGrid.PageSize);
            productId.Text = products[productSelected].ProductId.ToString();
            Name.Text = products[productSelected].Name.ToString();
            Category.Text = products[productSelected].Category.ToString();
            Response.Redirect("ProductSelected.aspx?ProductID=" + productId.Text + "&ProductName=" + Name.Text + "&ProductCategory=" +Category.Text);
            
        }
    }
}
